package com.dyz.leetcode._01_array;

public class _498_findDiagonalOrder {
    public int[] findDiagonalOrder(int[][] mat) {
        //col>=n:row+=2,col=n-1，换方向
        //row>=m:row=m-1,col+=2,换方向
        //col<0:row不变，col=0,换方向
        //row<o:row=0, col不变，换方向
        int m = mat.length;
        int n = mat[0].length;
        int row = 0;
        int col = 0;
        int[] res = new int[m*n];
        int[][] dirs = {{-1, 1}, {1, -1}};
        int di = 0;
        for(int i=0; i<m*n; i++){
            res[i] = mat[row][col];
            row = row + dirs[di][0];
            col = col + dirs[di][1];

            if (col >= n) {col = n - 1; row += 2; di = 1 - di;}
            if (row >= m) {row = m - 1; col += 2; di = 1 - di;}
            if (row < 0) {row = 0; di = 1 - di;}
            if (col < 0) {col = 0; di = 1 - di;}
        }

        return res;
    }
}
